package com.solvePlat.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.solvePlat.dao.WorkInfoCommentMapper;
import com.solvePlat.entity.WorkInfo;
import com.solvePlat.entity.WorkInfoComment;
import com.solvePlat.entity.WorkInfoCommentExample;
import com.solvePlat.shiro.JWTToken;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class WorkInfoCommentServiceImpl {
    @Resource
    private WorkInfoCommentMapper workInfoCommentMapper;

    public PageInfo selectWorkInfoComment(WorkInfo workInfo){

        PageHelper.startPage(workInfo.getPageNum(),workInfo.getPageSize());
        JWTToken jwtToken = (JWTToken) SecurityUtils.getSubject().getPrincipal();
        List<WorkInfoComment> workInfoCommentList =  workInfoCommentMapper.selectWorkInfoCommentWithUserInfoByWorkInfo(workInfo.getUuid(),jwtToken.getUserId(),"");
        PageInfo<WorkInfoComment> pageInfo = new PageInfo<WorkInfoComment>(workInfoCommentList);
        return pageInfo;
    }

    public void saveWorkInfoCommentInfo(WorkInfoComment workInfoComment){
        if (StringUtil.isEmpty(workInfoComment.getUuid())){
            JWTToken jwtToken = (JWTToken) SecurityUtils.getSubject().getPrincipal();
            workInfoComment.setCreatorid(jwtToken.getUserId());
            workInfoComment.setParent(workInfoComment.getWorkinfoid());
            workInfoComment.setUuid(UUID.randomUUID().toString());
            workInfoComment.setType("2");//发布的任务信息
            workInfoCommentMapper.insert(workInfoComment);
        }else{
            //更新评论信息
            WorkInfoCommentExample workInfoCommentExample = new WorkInfoCommentExample();
            workInfoCommentExample.createCriteria().andUuidEqualTo(workInfoComment.getUuid());
            workInfoCommentMapper.updateByExampleSelective(workInfoComment,workInfoCommentExample);
        }



    }

    public  WorkInfoComment getMeWorkInfoCommentInfo(WorkInfoComment workInfoComment){
        JWTToken jwtToken = (JWTToken) SecurityUtils.getSubject().getPrincipal();
        WorkInfoCommentExample workInfoCommentExample = new WorkInfoCommentExample();
        workInfoCommentExample.createCriteria().andTypeEqualTo("2")
                                                .andWorkinfoidEqualTo(workInfoComment.getWorkinfoid())
                                                .andCreatoridEqualTo(jwtToken.getUserId());

        WorkInfoComment workInfoCommentResult = workInfoCommentMapper.selectByExample(workInfoCommentExample).get(0);
        return workInfoCommentResult;
    }

    public PageInfo selectMeWorkInfoComment(WorkInfo workInfo){
        JWTToken jwtToken = (JWTToken) SecurityUtils.getSubject().getPrincipal();
        if(StringUtils.isEmpty(workInfo.getCreaterid())){
            workInfo.setCreaterid(jwtToken.getUserId());
        }
        List<WorkInfoComment> workInfoCommentList = workInfoCommentMapper.selectWorkInfoCommentWithUserInfoAndWorkINfoByWorkInfo(workInfo.getUuid(),jwtToken.getUserId(),workInfo.getCreaterid());
        PageInfo<WorkInfoComment> pageInfo = new PageInfo<WorkInfoComment>(workInfoCommentList);
        return pageInfo;
    }
}
